<?php

/**
 * Class that handles all the loading of resources
 */

class loader {
	private $aether;
	private $models;
	private $helpers;
	private $error;

	function __construct($aether) {
		$this->aether = $aether;
		$this->error = $aether->errorHandler;
	}
		

	/**
	 * Load resources
	 *
	 * @access  public
	 * @param   string   [$resource]      Gives the resource string in the format "module:type/name".  All three but the last are required.
	 * @param   string   [$defaultModule] Default module if none is specified.  Pass null to force module to be specified in resource string.  Defaults to null.
	 * @param   string   [$defaultType]   Default type if none is specified.  Pass null to force type to be specified in resource string.  Defaults to null.
	 * @param   boolean  [$triggerError]    If true, a resourceNotFound error will be triggered upon failure.  If false, a value of false will be returned
	 * @return  mixed    Will return false on failure if $triggerError is false.  On success, $className will be returned.
	 */
	function load($resource, $defaultModule = null, $defaultType = null, $triggerError = true) {
		$object = explode(':', $resource);
		if(!isset($object[1])) {
			if($defaultModule) {
				if($defaultModule=='this') {
					$module = $this->aether->getModule();
				} else {
					$module = $defaultModule;
				}
				$object = $object[0];
			} else {
				$this->error->trigger('resourceModuleNotSpecified', array('resource' => $resource));
			}
		} else {
			$module = $object[0];
			$object = $object[1];
		}
		$object = explode('/', $object);
		if(!isset($object[1])) {
			if($defaultType) {
				$type = $defaultType;
				$name = $object[0];
			} else {
				$this->error->trigger('resourceTypeNotSpecified', array('resource' => $resource));
			}
		} else {
			$type = $object[0];
			$name = $object[1];
		}
		$fn = ''; //Filename
		switch($module) {
			case 'app':
				$fn = 'app';
				break;
			case 'core':
				$fn = 'core';
				break;
			default:
				$fn = 'plugins/'.$module;
		}
		$fn .= '/';
		$fn2 = $name; //Filename, part 2
		switch($type) {
			case 'controller':
			case 'controllerHelper':
				$fn .= 'controllers';
				$fn2 .= 'Controller';
				break;
			case 'helper':
				$fn .= 'helpers';
				$fn2 .= 'Helper';
				break;
			case 'model':
				$fn .= 'models';
				$fn2 .= 'Model';
				break;
			case 'element':
				$fn .= 'elements';
				break;
			case 'class':
				$fn .= 'classes';
				break;
			case 'template':
				$fn .= 'templates';
				break;
		}
		$className = $fn2;
		$fn .= '/'.$fn2.'.php';
		if(file_exists($fn)) {
			if($type=='element' || $type=='view') {
				require($fn);
			} else if($type=='template') {
				return $fn;
			} else {
				require_once($fn);
			}
		} else {
			if($triggerError) {
				$this->error->trigger('resourceNotFound', array(
					'filename' => $fn,
					'module' => $module,
					'type' => $type,
					'name' => $name
				));
			} else {
				return false;
			}
		}
		switch($type) {
			case 'helper':
			case 'controllerHelper':
				$this->aether->helpers->add(new $className($this->aether), $name);
				break;
			case 'model':
				$this->aether->models->add(new $className(), $name);
				break;
		}
		return $className;
	}

	/**
	 * Loads multiple resources
	 *
	 * @access  public
	 * @param   array   [$array]         Array of resources wanted to load.
	 * @param   string  [$defaultModule] This is passed on to the load() method.
	 * @param   string  [$defaultType]   This is passed on to the load() method. 
	 * @return  array   Array of return values
	 */
	function loadArray(array $array, $defaultModule, $defaultType) {
		$return = array();
		foreach($array as $key => $value) {
			$return[$key] = $this->load($value, $defaultModule, $defaultType);
		}
		return $return;
	}
}

//EOF
